package com.scau402.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.StringUtils;
import com.scau402.common.utils.code.ClientCodeUtils;
import com.scau402.common.utils.code.UserCodeUtils;
import com.scau402.system.domain.SysTenant;
import com.scau402.system.mapper.SysTenantMapper;
import com.scau402.system.service.ISysTenantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.scau402.common.annotation.Log;
import com.scau402.common.core.controller.BaseController;
import com.scau402.common.core.domain.AjaxResult;
import com.scau402.common.enums.BusinessType;
import com.scau402.system.domain.WmsBasicClient;
import com.scau402.system.service.IWmsBasicClientService;
import com.scau402.common.utils.poi.ExcelUtil;
import com.scau402.common.core.page.TableDataInfo;

/**
 * 客户资料Controller
 * 
 * @author ruoyi
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/system/basicClient")
public class WmsBasicClientController extends BaseController
{
    @Autowired
    private IWmsBasicClientService wmsBasicClientService;

    @Autowired
    private ISysTenantService tenantService;

    /**
     * 查询客户资料列表
     */
    @PreAuthorize("@ss.hasPermi('system:basicClient:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsBasicClient wmsBasicClient)
    {
        startPage();
        List<WmsBasicClient> list = wmsBasicClientService.selectWmsBasicClientList(wmsBasicClient);
        return getDataTable(list);
    }

    /**
     * 导出客户资料列表
     */
    @PreAuthorize("@ss.hasPermi('system:basicClient:export')")
    @Log(title = "客户资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsBasicClient wmsBasicClient)
    {
        List<WmsBasicClient> list = wmsBasicClientService.selectWmsBasicClientList(wmsBasicClient);
        ExcelUtil<WmsBasicClient> util = new ExcelUtil<WmsBasicClient>(WmsBasicClient.class);
        util.exportExcel(response, list, "客户资料数据");
    }

    /**
     * 获取客户资料详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:basicClient:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsBasicClientService.selectWmsBasicClientById(id));
    }

    /**
     * 新增客户资料
     */
    @PreAuthorize("@ss.hasPermi('system:basicClient:add')")
    @Log(title = "客户资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsBasicClient wmsBasicClient)
    {
        // 判断客户编号是否存在
        wmsBasicClient.setTenantId(getLoginUser().getTenantId());
        if (StringUtils.isNotEmpty(wmsBasicClient.getClientCode()) && !wmsBasicClientService.checkClientCodeUnique(wmsBasicClient))
        {
            return error("新增客户'" + wmsBasicClient.getClientCode() + "'失败，客户编号已存在");
        }
        // 判断客户名称是否存在
        if (StringUtils.isNotEmpty(wmsBasicClient.getClientName()) && !wmsBasicClientService.checkClientNameUnique(wmsBasicClient))
        {
            return error("新增客户'" + wmsBasicClient.getClientName() + "'失败，客户名称已存在");
        }
        wmsBasicClient.setCreateBy(getUsername());
        return toAjax(wmsBasicClientService.insertWmsBasicClient(wmsBasicClient));
    }

    /**
     * 修改客户资料
     */
    @PreAuthorize("@ss.hasPermi('system:basicClient:edit')")
    @Log(title = "客户资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsBasicClient wmsBasicClient)
    {
        return toAjax(wmsBasicClientService.updateWmsBasicClient(wmsBasicClient));
    }

    /**
     * 删除客户资料
     */
    @PreAuthorize("@ss.hasPermi('system:basicClient:remove')")
    @Log(title = "客户资料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsBasicClientService.deleteWmsBasicClientByIds(ids));
    }

    /**
     * 启用、停用客户
     * @param wmsBasicClientList
     * @return
     */
    @Log(title = "客户资料", businessType = BusinessType.UPDATE)
    @PostMapping("/startOrStop")
    public AjaxResult startOrStop(@RequestBody List<WmsBasicClient> wmsBasicClientList){
        return toAjax(wmsBasicClientService.startOrStop(wmsBasicClientList));
    }

    /**
     * 获取客户编码
     * @return
     * @throws InterruptedException
     */
    @PreAuthorize("@ss.hasPermi('system:basicClient:add')")
    @GetMapping("/getClientCode")
    public String getClientCode() throws InterruptedException {
        // 返回租户编码
        return wmsBasicClientService.getNewCode(SecurityUtils.getLoginUser().getUser());
    }
}
